package cn.starlightsoftware.sherly.controller.admin.permission;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.starlightsoftware.sherly.controller.admin.permission.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.starlightsoftware.sherly.convertor.RoleConvertor;
import cn.starlightsoftware.sherly.model.PageResult;
import cn.starlightsoftware.sherly.model.Result;
import cn.starlightsoftware.sherly.db.model.permission.RoleDO;
import cn.starlightsoftware.sherly.db.model.permission.RolePermissionDO;
import cn.starlightsoftware.sherly.service.permission.RoleService;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.starlightsoftware.sherly.model.Result.success;

/**
 * @author 谷子毅
 */
@Validated
@Tag(name = "admin-角色")
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @GetMapping("/page")
    @PreAuthorize("@ss.hasAnyScopes('system:role:page')")
    @Operation(summary = "角色分页")
    public Result<PageResult<RoleRespVO>> page(@ParameterObject RolePageReqVO reqVO) {
        PageResult<RoleDO> pageResult = roleService.page(reqVO);
        if (CollUtil.isEmpty(pageResult.getResult())) {
            return success(PageResult.buildEmpty());
        }
        return success(PageResult.build(BeanUtil.copyToList(pageResult.getResult(), RoleRespVO.class), pageResult.getTotal()));
    }

    @PostMapping("/save")
    @PreAuthorize("@ss.hasAnyScopes('system:role:save')")
    @Operation(summary = "角色新增")
    public Result<?> save(@Valid @RequestBody RoleSaveReqVO reqVO) {
        roleService.save(reqVO);
        return success();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasAnyScopes('system:role:update')")
    @Operation(summary = "角色修改")
    public Result<?> update(@Valid @RequestBody RoleSaveReqVO reqVO) {
        roleService.update(reqVO);
        return success();
    }

    @GetMapping("/detail")
    @PreAuthorize("@ss.hasAnyScopes('system:role:detail')")
    @Operation(summary = "角色详情")
    public Result<RoleRespVO> detail(@ParameterObject RoleDetailReqVO reqVO) {
        RoleDO role = roleService.detail(reqVO.getRoleId());
        return success(BeanUtil.copyProperties(role, RoleRespVO.class));
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasAnyScopes('system:role:delete')")
    @Operation(summary = "角色删除")
    public Result<?> delete(@Valid @RequestBody RoleDetailReqVO reqVO) {
        roleService.delete(reqVO.getRoleId());
        return success();
    }

    @GetMapping("/permission-list")
    @PreAuthorize("@ss.hasAnyScopes('system:role:permission-list')")
    @Operation(summary = "角色权限详情")
    public Result<RolePermissionRespDTO> permissionList(@ParameterObject RoleDetailReqVO reqVO) {
        List<RolePermissionDO> rolePermissionList = roleService.rolePermissionList(reqVO.getRoleId());
        return success(RoleConvertor.buildRolePermissionRespVO(rolePermissionList));
    }

    @PostMapping("/permission-update")
    @PreAuthorize("@ss.hasAnyScopes('system:role:permission-update')")
    @Operation(summary = "角色权限修改")
    public Result<?> permissionUpdate(@Valid @RequestBody RolePermissionUpdateReqVO reqVO) {
        roleService.rolePermissionUpdate(reqVO);
        return success();
    }

    @GetMapping("simple-list")
    @Operation(summary = "角色精简列表", description = "用于角色选择")
    public Result<List<RoleSimpleRespVO>> simpleList() {
        List<RoleDO> roles = roleService.list();
        return success(BeanUtil.copyToList(roles, RoleSimpleRespVO.class));
    }

}
